package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.AdknowlDetail;
import org.dtrd.modules.evaluation.entity.bean.AdknowlListInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdAdknowl;
import org.dtrd.modules.evaluation.entity.request.AdknowlListRequest;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdAdknowlMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdAdknowlService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 患者知识量表（ADKnowl） 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-15
 */
@DS("multi-datasource1")
@Service
public class DtrdEntEvaluationRdAdknowlServiceImpl extends ServiceImpl<DtrdEntEvaluationRdAdknowlMapper, DtrdEntEvaluationRdAdknowl> implements IDtrdEntEvaluationRdAdknowlService {

    @Override
    public List<AdknowlListInfo> getAdknowlList(AdknowlListRequest request) {
        LambdaQueryWrapper<DtrdEntEvaluationRdAdknowl> queryWrapper = baseQueryWrapper();
        queryWrapper.eq(DtrdEntEvaluationRdAdknowl::getPatientId, request.getPatientId())
                    .orderByDesc(DtrdEntEvaluationRdAdknowl::getCreateTime);
        List<DtrdEntEvaluationRdAdknowl> poList = list(queryWrapper);
        List<AdknowlListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(AdknowlListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public AdknowlDetail getAdknowlLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdAdknowl> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdAdknowl.class)
                .eq(DtrdEntEvaluationRdAdknowl::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdAdknowl::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdAdknowl dtrdEntEvaluationRdAdknowl = getOne(queryWrapper);
        return Optional.ofNullable(dtrdEntEvaluationRdAdknowl)
                        .map(po -> new AdknowlDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public AdknowlDetail saveOrUpdateAdknowl(AdknowlDetail dto) {
        DtrdEntEvaluationRdAdknowl dtrdEntEvaluationRdAdknowl = new DtrdEntEvaluationRdAdknowl();
        if(dto.getDataId() == null){
            dtrdEntEvaluationRdAdknowl = dtrdEntEvaluationRdAdknowl.init();
        }
        dtrdEntEvaluationRdAdknowl.parseFromDto(dto);
        // 总分暂时把全部的分数加起来
        int score = 0;
        List<Integer> inputs = dto.getInput();
        for (Integer input : inputs) {
            score += input;
        }
        dto.setScore(score);
        if (dto.getPatientId() != null) {
            save(dtrdEntEvaluationRdAdknowl);
        }
        dto.setOutput("良好");
        return dto;
    }

    @Override
    public boolean removeAdknowl(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntEvaluationRdAdknowl> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntEvaluationRdAdknowl.class)
                        .eq(DtrdEntEvaluationRdAdknowl::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntEvaluationRdAdknowl> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntEvaluationRdAdknowl.class)
                        .eq(DtrdEntEvaluationRdAdknowl::getIsDel, 0);
    }
}
